package com.lw.vue_shop_server.service.impl;

import com.lw.vue_shop_server.mapper.GoodMapper;
import com.lw.vue_shop_server.po.Good;
import com.lw.vue_shop_server.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodServiceImpl implements GoodService {

    @Autowired
    public GoodMapper goodMapper;

    @Override
    public Map getGoodnum() {
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> data = new HashMap<>();
        Map<String,Object> meta = new HashMap<>();
        Integer total = goodMapper.getGoodnum();
        if(total != null){
            data.put("total",total);
            meta.put("msg","获取成功");
            meta.put("status",200);
        }else {
            meta.put("msg","获取失败");
            meta.put("status",500);
        }
        result.put("meta",meta);
        result.put("data",data);
        return result;
    }

    @Override
    public Map getGoodList(Integer pagenum, Integer pagesize) {
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> data = new HashMap<>();
        Map<String,Object> meta = new HashMap<>();
        List<Good> goods = goodMapper.getGoodList((pagenum-1)*pagesize,pagesize);
        if(goods != null){
            data.put("goods",goods);
            data.put("pagenum",pagenum);
            data.put("total",goodMapper.getGoodnum());
            meta.put("msg","获取成功");
            meta.put("status",200);
        }else {
            meta.put("msg","获取失败");
            meta.put("status",500);
        }
        result.put("data",data);
        result.put("meta",meta);
        return result;
    }

    @Override
    public Map delGoodById(Integer id) {
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> meta = new HashMap<>();
        int row = goodMapper.delGoodById(id);
        if(row>0){
            result.put("data",null);
            meta.put("msg","删除成功");
            meta.put("status",200);
        }else {
            meta.put("msg","删除失败");
            meta.put("status",200);
        }
        result.put("meta",meta);
        return result;
    }
}
